package mypackage;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Resource{
    private int flag = 1;
    private final Lock lock = new ReentrantLock();
    private final Condition con1 = lock.newCondition();
    private final Condition con2 = lock.newCondition();
    private final Condition con3 = lock.newCondition();

    public void print5(int loop) throws InterruptedException{
        lock.lock();
        try {
            while (flag != 1){
                con1.await();
            }
            for(int i=0;i<5;i++){
                System.out.println(Thread.currentThread().getName()+" :: "+(i+1)+" ：轮数："+(loop+1));
            }
            flag = 2;
            con2.signal();
        }finally {
            lock.unlock();
        }
    }

    public void print10(int loop) throws InterruptedException{
        lock.lock();
        try {
            while (flag != 2){
                con2.await();
            }
            for(int i=0;i<10;i++){
                System.out.println(Thread.currentThread().getName()+" :: "+(i+1)+" ：轮数："+(loop+1));
            }
            flag = 3;
            con3.signal();
        }finally {
            lock.unlock();
        }
    }

    public void print15(int loop) throws InterruptedException{
        lock.lock();
        try {
            while (flag != 3){
                con3.await();
            }
            for(int i=0;i<15;i++){
                System.out.println(Thread.currentThread().getName()+" :: "+(i+1)+" ：轮数："+(loop+1));
            }
            flag = 1;
            con1.signal();
        }finally {
            lock.unlock();
        }
    }
}
public class LockCondition {

    public static void main(String[] args) {
        test1();
    }

    private static void test1() {
        Resource resource = new Resource();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<10;i++){
                    try {
                        resource.print5(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"AA").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<10;i++){
                    try {
                        resource.print10(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"BB").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<10;i++){
                    try {
                        resource.print15(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"CC").start();
    }
}
